﻿using System;
using System.Data;
using System.Text;
namespace WPFExcelReport.Helpers
{
    /// <summary>
    /// VisualHelper convert the given visual content as an image
    /// </summary>
    public static partial class Helper
    {
      
        public static string CHAR2STR(char[] str)
        {
            int lg = 0;
            string val = "";
            val = "";
            string str2 = "";
            for (int i = 0; i < str.Length; i++)
            {
                str2 = str[i].ToString();
                if (str2 == "")
                    break;
                val += str2;
            } 
            return val;
        }
        public static char[] STR2CHAR(string str)
        {
            char[] p=str.ToCharArray(); 
            return p;

        }
        public static string CHAR2MAC(char[] str)
        {

            byte[] encryptedBytes = Encoding.ASCII.GetBytes(str);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < encryptedBytes.Length; i++)
            {
                sb.AppendFormat("{0:x2}", encryptedBytes[i]);
            }
            return sb.ToString().ToUpper();
            //return val;
        } 
        public static long IpToInt(string ip)
        {
            char[] separator = new char[] { '.' };
            string[] items = ip.Split(separator);
            return long.Parse(items[0])
                    | long.Parse(items[1]) << 8
                    | long.Parse(items[2]) << 16
                    | long.Parse(items[3]) << 24;
        }
        public static string IntToIp(long ipInt)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append((ipInt) & 0xFF).Append(".");
            sb.Append((ipInt >> 8) & 0xFF).Append(".");
            sb.Append((ipInt >> 16) & 0xFF).Append(".");
            sb.Append(ipInt >> 24 & 0xFF);
            return sb.ToString();
        }

        public static double RoundX(double input, double times)
        {
           
            return Math.Round(input*times, MidpointRounding.AwayFromZero) / times;
        }
        public static string GetDateAddDays(string eDate, int LeadTime)
        {
            DateTime date = DateTime.Now;
            string sDate = "";
            try
            {
                int day = System.Convert.ToInt16(Right(eDate, 2));
                int year = System.Convert.ToInt16(Left(eDate, 4));
                int mon = System.Convert.ToInt16(eDate.Substring(4, 2));
                date = new DateTime(year, mon, day);
                date = date.AddDays(LeadTime);
                sDate = date.Year.ToString() + Right("00" + date.Month.ToString(), 2) + Right("00" + date.Day.ToString(), 2);
            }
            catch (Exception ex)
            {
                 
            }
            return sDate;
        }
        public static string Convert8859P1ToGB2312(string s)
        {
            return System.Text.Encoding.Default.GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(s));
        }
        public static string ConvertGB2312To8859P1(string s)
        {
            byte[] b;
            string filevalue = "";
            b = Encoding.GetEncoding("gb2312").GetBytes(s);
            filevalue = Encoding.GetEncoding("ISO-8859-1").GetString(b);
            return filevalue;
        }
        public static string GetGB2312Value(string strSource)
        {
            byte[] b;
            string filevalue = "";
            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(strSource);
            filevalue = Encoding.GetEncoding("gb2312").GetString(b);
            return filevalue;
        }
        public static string GetGB2312Value(DataRow dwTemp, string nmField)
        {
            byte[] b;
            string filevalue = "";
            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[nmField].ToString());
            filevalue = Encoding.GetEncoding("gb2312").GetString(b);
            return filevalue;
        }
        public static string GetGB2312Value(string strSource, string nmField)
        {
            byte[] b;
            string filevalue = "";
            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(strSource);
            filevalue = Encoding.GetEncoding("gb2312").GetString(b);
            return filevalue;
        }
        public static string Right(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str.Substring(leng - num, num);
            }
            catch (Exception ex)
            {
            }
            return str;
        }
        public static string Left(string str, int num)
        { 
            int leng = str.Length;
            str = str + "                                                                                ";
            str = str.Substring(0, num);

            return str;
        }
        public static string Times(string str, double precision)
        {
            return (System.Convert.ToDouble(str) * precision).ToString(); 
        }
        public static string StringLG(string str, int target)
        {
            int length = Encoding.Default.GetByteCount(str.Trim());
            if (target > length)
            {
                str = str.PadRight(target - length);
            }
            else
                str = Left(str, target);
            return str;
        }

        public static object MathCalculate(string expression)
        {
            object retvar = null;
            Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
            System.CodeDom.Compiler.CompilerParameters cp = new System.CodeDom.Compiler.CompilerParameters(
            new string[] { @"System.dll" });
            StringBuilder builder = new StringBuilder("using System;class CalcExp{public static object Calc(){ return \"expression\";}}");
            builder.Replace("\"expression\"", expression);
            string code = builder.ToString();
            System.CodeDom.Compiler.CompilerResults results;
            results = provider.CompileAssemblyFromSource(cp, new string[] { code });
            if (results.Errors.HasErrors)
            {
                retvar = null;
            }
            else
            {
                System.Reflection.Assembly a = results.CompiledAssembly;
                Type t = a.GetType("CalcExp");
                retvar = t.InvokeMember("Calc", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.InvokeMethod
                    , System.Type.DefaultBinder, null, null);
            }
            return retvar;
        } 
    }

}
